package jfdi;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint.Style;
import android.graphics.Rect;

import util.Mlog;
import jfdi.event.ClickDetector;

public class Button extends Label implements ClickDetector.ClickHandler {

    private ClickDetector mClicker;
    private Rect mBounds;
    private boolean mSelected;
    
    public Button(NodeHost host) {
        super(host);
    }
    
    @Override
    protected void init() {
        super.init();
        mClicker = new ClickDetector(this);
        mHost.addTouchListener(mClicker);
        mBounds = new Rect();
    }
    
    @Override
    public void layout(int l, int t, int r, int b) {
        super.layout(l, t, r, b);
        mBounds.left = mLeft;
        mBounds.top = mTop;
        mBounds.right = mRight;
        mBounds.bottom = mBottom;
    }
    
    @Override
    public void render(Canvas canvas) {
        int bg = mSelected ? Color.CYAN : Color.LTGRAY;
        mPaint.setColor(bg);
        mPaint.setStyle(Style.FILL);
        canvas.drawRect(0, 0, getWidth(), getHeight(), mPaint);
        super.render(canvas);
    }

    // ClickHandler

    @Override
    public Rect getBounds() {
        return mBounds;
    }

    @Override
    public void onSelect(int x, int y) {
        if (!mSelected) {
            mSelected = true;
            mHost.invalidate();
        }
        
    }

    @Override
    public void onDeselect(int x, int y) {
        if (mSelected) {
            mSelected = false;
            mHost.invalidate();
        }
    }

    @Override
    public void onClick(int x, int y) {
        Mlog.i("button", "Button ", this, " was clicked!");
    }
}
